package client

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"onerk4/zk2/common/proto/user"
)

type UserHandler func(ctx context.Context, in user.UserClient) (interface{}, error)

func UserClient(ctx context.Context, handler UserHandler) (interface{}, error) {
	// 1.连接
	conn, err := grpc.Dial("127.0.0.1:8081", grpc.WithInsecure())
	if err != nil {
		fmt.Printf("连接异常： %s\n", err)
		return nil, err
	}
	defer conn.Close()
	client := user.NewUserClient(conn)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	return res, nil
}

func Login(ctx context.Context, req *user.LoginRequest) (*user.LoginResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		login, err := in.Login(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.LoginResponse), nil
}

func CourseList(ctx context.Context, req *user.CourseListRequest) (*user.CourseListResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		login, err := in.CourseList(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.CourseListResponse), nil
}

func CourseInfo(ctx context.Context, req *user.CourseInfoRequest) (*user.CourseInfoResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		login, err := in.CourseInfo(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.CourseInfoResponse), nil
}

func ReservationCourse(ctx context.Context, req *user.ReservationCourseRequest) (*user.ReservationCourseResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		login, err := in.ReservationCourse(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.ReservationCourseResponse), nil
}

func QueryReservationCourse(ctx context.Context, req *user.QueryReservationCourseRequest) (*user.QueryReservationCourseResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		login, err := in.QueryReservationCourse(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.QueryReservationCourseResponse), nil
}

func QueryReservationCourseInfo(ctx context.Context, req *user.QueryReservationCourseRequestInfoRequest) (*user.QueryReservationCourseResponseInfoResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		login, err := in.QueryReservationCourseInfo(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.QueryReservationCourseResponseInfoResponse), nil
}

func DelQueryReservation(ctx context.Context, req *user.DelQueryReservationRequest) (*user.DelQueryReservationResponse, error) {
	client, err := UserClient(ctx, func(ctx context.Context, in user.UserClient) (interface{}, error) {
		login, err := in.DelQueryReservation(ctx, req)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*user.DelQueryReservationResponse), nil
}
